/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package android.entidades;

import android.app.ContextoAndroid;
import android.app.Mensaje;
import android.content.Context;
import android.content.ContextWrapper;
import android.sql.AbstractAndroid;
import android.sql.UsuarioPensamiento;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author efraingutierrez
 */
public class Data 
{
    
    public static ArrayList<UsuarioPensamiento> getData()
    {
        List<AbstractAndroid> lst = UsuarioPensamiento.getAll("UsuarioPensamiento", "IdPensamiento", UsuarioPensamiento.class);
        ArrayList<UsuarioPensamiento> ls = new ArrayList<UsuarioPensamiento>();
        for(AbstractAndroid abs : lst)
        {
            UsuarioPensamiento us = (UsuarioPensamiento)abs;
            ls.add(us);
        }
        if(Data.getID() > ls.size())
            Data.resetID();
        DataLocal.saveDataLocal(lst.size()+"");
        return ls;
    }
    
    private static final String userFile = "dataid.txt";
     
    public static boolean existeArchivo()
    {
        ContextWrapper cw = new ContextWrapper(ContextoAndroid.context);
        File directory = cw.getDir("media", Context.MODE_PRIVATE);
        String path = directory.getAbsolutePath()+"/"+userFile;
        try {
            File myFile = new File(path);
            FileInputStream fIn = new FileInputStream(myFile);
            BufferedReader myReader = new BufferedReader(new InputStreamReader(fIn));
            myReader.close();
            return true;
        } 
        catch (Exception e) {
            return false;
        }
    }
    
    public static int getID()
    {
        ContextWrapper cw = new ContextWrapper(ContextoAndroid.context);
        File directory = cw.getDir("media", Context.MODE_PRIVATE);
        String path = directory.getAbsolutePath()+"/"+userFile;
        try {
            File myFile = new File(path);
            FileInputStream fIn = new FileInputStream(myFile);
            BufferedReader myReader = new BufferedReader(new InputStreamReader(fIn));
            String aDataRow = "";
            String aBuffer = "";
            while ((aDataRow = myReader.readLine()) != null) 
            {
                aBuffer += aDataRow;
            }
            int id = Integer.parseInt(aBuffer);
            myReader.close();
            return id;
        } 
        catch (Exception e) 
        {
            return 0;
        }
    }
    
    public static void incrementID() 
    {
        int ID = getID();
        ContextWrapper cw = new ContextWrapper(ContextoAndroid.context);
        File directory = cw.getDir("media", Context.MODE_PRIVATE);
        try
        {
            String path = directory.getAbsolutePath()+"/"+userFile;
            FileWriter logWriter = new FileWriter(path);                  
            BufferedWriter out = new BufferedWriter(logWriter);
            ID++;
            out.write(""+ID);
            out.close();
        } 
        catch (Exception e) {
            Mensaje.Mostrar(e.toString());
        }
    }
    
    public static void decreaseID() 
    {
        int ID = getID();
        ContextWrapper cw = new ContextWrapper(ContextoAndroid.context);
        File directory = cw.getDir("media", Context.MODE_PRIVATE);
        try
        {
            String path = directory.getAbsolutePath()+"/"+userFile;
            FileWriter logWriter = new FileWriter(path);                  
            BufferedWriter out = new BufferedWriter(logWriter);
            ID--;
            out.write(""+ID);
            out.close();
        } 
        catch (Exception e) {
            Mensaje.Mostrar(e.toString());
        }
    }
    
    public static void resetID() 
    {
        ContextWrapper cw = new ContextWrapper(ContextoAndroid.context);
        File directory = cw.getDir("media", Context.MODE_PRIVATE);
        try
        {
            String path = directory.getAbsolutePath()+"/"+userFile;
            FileWriter logWriter = new FileWriter(path);                  
            BufferedWriter out = new BufferedWriter(logWriter);
            out.write("0");
            out.close();
        } 
        catch (Exception e) {
            Mensaje.Mostrar(e.toString());
        }
    }
    
    public static void deleteID() 
    {
        ContextWrapper cw = new ContextWrapper(ContextoAndroid.context);
        File directory = cw.getDir("media", Context.MODE_PRIVATE);
        try
        {
            String path = directory.getAbsolutePath()+"/"+userFile;
            File myFile = new File(path);
            myFile.delete();
        } 
        catch (Exception e) {
            
        }
    }
      
}
